// ====================EffectCardContainer====================

class EffectCardContainer extends CardContainerBase {
    initialize() {
        super.initialize();
        /**
         * 卡牌效果精灵池
         * @type {Sprite_EffectCard[]}
         * @private
         */
        this._cardSpritePool = [];
        /**
         * 使用中的卡牌效果精灵
         * @type {Sprite_EffectCard[]}
         * @private
         */
        this._currentCardSprite = [];
        this._vanish = null;
        this.createTitle();
    }

    createTitle() {
        const {width} = this._opts;
        const title = new Sprite(new Bitmap(width, 50));
        title.anchor.set(0.5, 1);
        title.x = width / 2;
        title.y = -32;
        title.bitmap.fontFace = $gameSystem.mainFontFace();
        title.bitmap.fontSize = 32;
        this._title = title;
        this.addChild(this._title);
    }

    setTitle(text) {
        const bitmap = this._title.bitmap;
        bitmap.clear();
        bitmap.drawText(text, 0, 0, bitmap.width, 50, "center");
        this._title.show();
    }

    /**
     * 取出卡牌
     */
    retrieve() {
        let sprite;
        if (this._cardSpritePool.length > 0) {
            sprite = this._cardSpritePool.shift();
        } else {
            sprite = new Sprite_EffectCard({width: 200, height: 300, r: 10, rim: 3});
            this.addChild(sprite);
        }
        this._currentCardSprite.push(sprite);
        return sprite;
    }

    /**
     * 放回卡牌
     */
    put() {

    }

    refreshCardSprite() {
        const select = this._range.random(this._amount);
        for (const index of select) {
            const sprite = this._cardSpritePool.shift() || new Sprite_HandCard({width: 200, height: 300, r: 10, rim: 3});
            sprite.setObject(index);
            this.addChild(sprite);
            this._selectedCardSprite.push(sprite);
        }
        this.sort();
    }

    updateContainer() {
        this.updateVanish();
    }

    /**
     * 更新消失效果
     */
    updateVanish() {
        if (!!this._vanish) {
            this._vanish--;
            if (this._vanish === 0) {
                this._vanish = null;
                this.hide();
            }
        }
    }

    hide() {
        super.hide();
        for (const sprite of this._currentCardSprite) {
            sprite.clear();
            sprite.hide();
        }
        this._cardSpritePool.push(...this._currentCardSprite);
        this._currentCardSprite = [];
        this._title.hide();
    }

    destroy() {
        super.destroy();
    }

    setVanish(time) {
        this._vanish = time;
    }

    /**
     * 当手牌选择
     * @param {Sprite_HandCard} cardSprite 选择的卡牌精灵
     */
    onHandCardSelected(cardSprite) {
        if (!!this._eventSelect) {
            this._eventSelect(cardSprite);
            this._eventSelect = null;
        }
    }

    /**
     * 设置弃牌效果
     * @param {Game_Card} card 弃的牌
     */
    setupAbandonEffect(card) {
        // 计算弃牌精灵的本地坐标
        const point1 = this.toLocal(CardGameManager.deckPosition);
        const point2 = this.toLocal(CardGameManager.cemeteryPosition);
        const effectSprite = this.retrieve();
        effectSprite.show();
        effectSprite.setObject(card);
        effectSprite.move(point1.x, point1.y);
        effectSprite.setMoveAnimation(point2.x, point2.y, 40,20);
        effectSprite.setScaleAnimation(0.5, 0.5, 60);
        effectSprite.setBlurAnimation(122, 60);
        this.setVanish(75);
        this.show();
    }

    /**
     * 设置发现效果
     * @param {[Game_Card]} goods 发现列表
     */
    setupDiscoverEffect(goods) {
        const {width} = this._opts;
        const amount = goods.length;
        const cardWidth = 200;
        const y = 300;
        const spacing = 30;
        const margin = (width - (cardWidth + spacing) * amount + spacing) / 2;
        for (let i = 0; i < amount; i++) {
            const card = goods[i];
            const startPoint = this.toLocal(CardGameManager.deckPosition);
            const targetX = margin + i * (cardWidth + spacing) + cardWidth / 2;
            const effectSprite = this.retrieve();
            effectSprite.show();
            effectSprite.setObject(card);
            effectSprite.move(startPoint.x, startPoint.y);
            effectSprite.setMoveAnimation(targetX, y, 30);
            // 为效果牌添加点击事件，事件触发时会传入精灵自身作为第一个参数
            effectSprite.addEventListener("onClick", (obj) => {
                const state = obj.context.click;
                if (!!state) {
                    obj.hide();
                    // 该牌相对于手牌容器的相对坐标
                    const point = this.toGlobal(new Point(obj.x, obj.y));
                    const deskPoint = this.toLocal(CardGameManager.deckPosition);
                    this._handPanel.draw(obj.object(), point);
                    // 从牌库中移除该牌
                    CardGameManager.deck.remove(obj.object());
                    for (const sprite of this._currentCardSprite) {
                        sprite.setMoveAnimation(deskPoint.x, deskPoint.y, 30);
                        sprite.setScaleAnimation(0.5, 0.5, 30);
                    }
                    this.setVanish(40);
                } else {
                    for (const sprite of this._currentCardSprite) {
                        sprite.context.click = false;
                        sprite.setPopular();
                    }
                    obj.context.click = true;
                    obj.setFocus();
                }
            })
        }
        this.setTitle("请选择一张牌加入手牌");
        this.show();
    }

    /**
     * 设置选择手牌
     * @param {Function} callback 回调函数
     */
    setupHandCardSelect(callback) {
        this.setTitle("请选择一张手牌");
        this.show();
    }

    /**
     * 设置手牌面板
     * @param panel
     */
    setHandPanel(panel) {
        this._handPanel = panel;
    }
}